package com.jsmy.marketing.api;

import android.content.Context;
import android.util.Log;

import com.jsmy.marketing.base.BaseApplication;
import com.jsmy.marketing.bean.AppTJListBean;
import com.jsmy.marketing.bean.AppZLGroupBean;
import com.jsmy.marketing.bean.AppZLListBean;
import com.jsmy.marketing.bean.BaseBean;
import com.jsmy.marketing.bean.CustomerBean;
import com.jsmy.marketing.bean.GTKJListBean;
import com.jsmy.marketing.bean.GtrzLxBean;
import com.jsmy.marketing.bean.ListenerBean;
import com.jsmy.marketing.bean.LoginBean;
import com.jsmy.marketing.bean.NewsBean;
import com.jsmy.marketing.bean.PlanInfoBean;
import com.jsmy.marketing.bean.RankListBean;
import com.jsmy.marketing.bean.ResourceBean;
import com.jsmy.marketing.bean.StudentBean;
import com.jsmy.marketing.bean.TXLBean;
import com.jsmy.marketing.bean.UserInfo;
import com.jsmy.marketing.bean.VersionBean;
import com.jsmy.marketing.bean.YWgtjhBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.NetUtil;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;

/**
 * Created by PC_WLT on 2018/8/24.
 */

public class RetrofitManager {
    public final static int CONNECT_TIMEOUT = 5;
    public final static int READ_TIMEOUT = 100;
    public final static int WRITE_TIMEOUT = 60;
    private static GetApiInterface mGetApiInterface;
    private static Context context = BaseApplication.mInstance;

    public static GetApiInterface getInstance() {

        if (mGetApiInterface == null) {

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Link.URL_BOOT)
                    .client(getOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            mGetApiInterface = retrofit.create(GetApiInterface.class);
        }
        return mGetApiInterface;
    }

    /**
     * 设置连接器  设置缓存
     */
    public static Interceptor getNetWorkInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Response response = chain.proceed(request);
                if (NetUtil.isNetworkAvailable(BaseApplication.mInstance.getApplicationContext())) {
                    int maxAge = 0 * 60;
                    // 有网络时 设置缓存超时时间0个小时
                    response.newBuilder()
                            .header("Cache-Control", "public, max-age=" + maxAge)
                            .removeHeader("Pragma")
                            .build();
                } else {
                    // 无网络时，设置超时为1周
                    int maxStale = 60 * 60 * 24 * 7;
                    response.newBuilder()
                            .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                            .removeHeader("Pragma")
                            .build();
                }
                return response;
            }
        };
    }

    public static OkHttpClient getOkHttpClient() {
        //设置缓存路径
        File httpCacheDirectory = new File(context.getCacheDir(), "responses");
        //设置缓存 10M
        Cache cache = null;
        try {
            cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
        } catch (Exception e) {
            Log.e("OKHttp", "Could not create http cache", e);
            e.printStackTrace();
        }
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                Log.i("RetrofitLog", "retrofitBack = " + message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        //创建OkHttpClient，并添加拦截器和缓存代码
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)//设置写的超时时间
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)//设置连接超时时间
                .addNetworkInterceptor(getNetWorkInterceptor())
                .addInterceptor(loggingInterceptor)
                .cache(cache)
                .build();
        return client;
    }
    public interface GetApiInterface {
        @GET
        Observable<ResponseBody> baseGet(@Url String url, @QueryMap Map<String, String> maps);

        @POST
        Observable<ResponseBody> basePost(@Url String url, @QueryMap Map<String, String> maps);
        /**
         * 登录
         *
         * @param map
         * @return
         */
        @GET(Link.LOG_IN)
        Observable<LoginBean> login(@QueryMap Map<String, Object> map);
        /**
         * 首页资料
         * APP_ZL_LIST
         */
        @GET(Link.APP_ZL_LIST)
        Observable<AppZLListBean> getAppZlList(@QueryMap Map<String, Object> map);

        /**
         * 3首页--统计信息
         * @param map
         * @return
         */
        @GET(Link.APP_TJ_LIST)
        Observable<AppTJListBean> getAppTJList(@QueryMap Map<String, Object> map);

        /**
         * 保存分享信息
         * @param map
         * @return
         */
        @GET(Link.APP_SAVE_FX_INFO)
        Observable<BaseBean> saveFxInfo(@QueryMap Map<String, Object> map);

        /**
         * 共享学生列表
         * @param map
         * @return
         */
        @GET(Link.APP_STUDNET)
        Observable<StudentBean> studentList(@QueryMap Map<String, Object> map);

        /**
         * 抢单
         * @param map
         * @return
         */
        @GET(Link.APP_QD)
        Observable<BaseBean> studentqd(@QueryMap Map<String, Object> map);

        /**
         * 排行榜
         * @param map
         * @return
         */
        @GET(Link.APP_RANK_LIST)
        Observable<RankListBean> rankList(@QueryMap Map<String, Object> map);
        /**
         * APP_ZL_GROUP_LIST
         * 8首页--资料更多的组别选择
         */
        @GET(Link.APP_ZL_GROUP_LIST)
        Observable<AppZLGroupBean> getappzlgrouplist(@QueryMap Map<String, Object> map);

        /**
         * APP_ZL_GD_LIST
         * 5首页--更多资料
         */
        @GET(Link.APP_ZL_GD_LIST)
        Observable<AppZLListBean> getappsyzlgdlist(@QueryMap Map<String, Object> map);

        /**
         19业务--沟通计划
         */
        @GET(Link.APP_YW_GTJH_LIST)
        Observable<YWgtjhBean> getgtjhlist(@QueryMap Map<String, Object> map);

        /**
         20业务--沟通日志
         */
        @GET(Link.APP_YW_GTRZ_LIST)
        Observable<YWgtjhBean> getgtrzlist(@QueryMap Map<String, Object> map);
        /**
         * getzlgtplanlist
         * 10资料--任务完成与取消
         */
        @GET(Link.APP_SAVE_RW_FLAG_INFO)
        Observable<AppZLListBean> getjhwcinfo(@QueryMap Map<String, Object> map);

        /**
         * 17客户--删除沟通日志
         */
        @GET(Link.APP_SAVE_DEL_GTRZ_INFO)
        Observable<BaseBean> savedelgtrzinfo(@QueryMap Map<String, Object> map);

        /**
         * APP_TXL_LIST
         * //6通讯录
         */
        @GET(Link.APP_TXL_LIST)
        Observable<TXLBean> gettxllist(@QueryMap Map<String, Object> map);

        /**
         * pvertype
         */
        @GET(Link.APP_VERSION_INFO)
        Observable<VersionBean> getVersionInfo(@QueryMap Map<String, Object> map);

        @GET(Link.APP_ZILIAO_choose)
        Observable<NewsBean> getNews(@QueryMap Map<String, Object> map);

        @GET(Link.APP_GTKJ_LIST)
        Observable<GTKJListBean> getGTKJList(@QueryMap Map<String, Object> map);

        @GET(Link.APP_GTLX)
        Observable<GtrzLxBean> getgtkjlist(@QueryMap Map<String, Object> map);
        /**
         * APP_ZL_GROUP_LIST
         * 更换阶段
         */

        @GET(Link.APP_UPDATEFZ)
        Observable<BaseBean> updateFz(@QueryMap Map<String, Object> map);

        @GET(Link.APP_KHZL_INFO)
        Observable<CustomerBean> getkhzlinfo(@QueryMap Map<String, Object> map);

        /**
         * APP_ZLGT_LPAN_LIST
         * 12客户--沟通计划列表
         */
        @GET(Link.APP_ZLGT_LPAN_LIST)
        Observable<YWgtjhBean> getzlgtplanlist(@QueryMap Map<String, Object> map);

        /**
         * APP_SAVE_RW_FLAG_INFO
         */

        @GET(Link.APP_SAVE_RW_FLAG_INFO)
        Observable<BaseBean> saverwflaginfo(@QueryMap Map<String, Object> map);

        /**
         * getkhzlgtrzlist
         */
        @GET(Link.APP_KHZL_GTRZ_LIST)
        Observable<YWgtjhBean> getkhzlgtrzlist(@QueryMap Map<String, Object> map);

        @GET(Link.APP_getgrzllist)
        Observable<ResourceBean> getgrzllist(@QueryMap Map<String, Object> map);

        /**
         * 添加计划 APP_SAVE_GTPLAN_INFO
         */
        @GET(Link.APP_SAVE_GTPLAN_INFO)
        Observable<BaseBean> savegtplaninfo(@QueryMap Map<String, Object> map);

        /**
         * 选择资源
         * @param map
         * @return
         */
        @GET(Link.APP_ZILIAO_choose)
        Observable<NewsBean> requestNews(@QueryMap Map<String, Object> map);

        /**
         *添加日志 APP_SAVE_NEW_GTRZ_INFO
         */
        @GET(Link.APP_SAVE_NEW_GTRZ_INFO)
        Observable<NewsBean> saveNewsRzInfo(@QueryMap Map<String, Object> map);


        /**
         * 获取计划详情
         */
        @GET(Link.APP_GTJH_INFO)
        Observable<PlanInfoBean> getgtjhinfo(@QueryMap Map<String, Object> map);



        /**
         * 添加试听
         */
        @GET(Link.APP_insertxsst)
        Observable<BaseBean> insertxsst(@QueryMap Map<String, Object> map);

        /**
         * 学生试听类别
         * @param map
         * @return
         */
        @GET(Link.APP_getxsstlist)
        Observable<ListenerBean> getxsstlist(@QueryMap Map<String, Object> map);

        /**
         * 老师试听类别
         * @param map
         * @return
         */
        @GET(Link.APP_getlsstlist)
        Observable<ListenerBean> getlsstlist(@QueryMap Map<String, Object> map);


        /**
         * 试听完成APP_updatexsst
         *
         */
        @GET(Link.APP_updatexsst)
        Observable<BaseBean> updatexsst(@QueryMap Map<String, Object> map);


        /**
         * 获取老师资料
         * APP_getlsinfo
         */
        @GET(Link.APP_getlsinfo)
        Observable<UserInfo> getlsinfo(@QueryMap Map<String ,Object> map);


    }
}
